package com.esri.core.tasks.na;

import androidx.core.app.NotificationCompat;
import com.esri.core.internal.util.f;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Scanner;

/* loaded from: classes.dex */
class LocalRouteTask extends RouteTask {
    private static final String[] directions_languages;
    private static final String[] directions_styles;
    private long mAttributeHelper;
    private long mNativePtr;
    private NetworkDescription mDescription = null;
    private String mCachedDirectionsLanguage = null;
    private String mCachedDirectionsStyle = null;

    /* JADX INFO: Access modifiers changed from: package-private */
    public static native String[] augmentAttributes(long j, String str, String[] strArr, String[] strArr2, double[] dArr);

    /* JADX INFO: Access modifiers changed from: package-private */
    public static native long createAttributeHelper(long j, String[] strArr, String[] strArr2, String str, String[] strArr3, String str2);

    private native long createNativeRouteTask(String str, String str2);

    private native void dispose(long j);

    private native long getDefaultParameters(long j);

    private native NetworkDescription getNetworkDescription(long j);

    /* JADX INFO: Access modifiers changed from: package-private */
    public static native void releaseAttributeHelper(long j);

    private native void setDirectionsResource(long j, String str);

    private native long solve(long j, long j2, long j3, RouteParameters routeParameters);

    static {
        f.a().initialize();
        directions_languages = new String[]{"ar", "de", "en", "es", "fr", "he", "it", "ja", "ko", "lt", "nl", "pl", "pt-BR", "pt-PT", "ru", "sv", "zh-CN"};
        directions_styles = new String[]{"NA Desktop", "NA Campus", "NA Navigation"};
    }

    public LocalRouteTask(String str, String str2) throws Exception {
        this.mNativePtr = createNativeRouteTask(str, str2);
    }

    @Override // com.esri.core.tasks.na.RouteTask
    public RouteParameters retrieveDefaultRouteTaskParameters() throws Exception {
        LocalRouteParameters localRouteParameters = new LocalRouteParameters(getDefaultParameters(this.mNativePtr));
        localRouteParameters.setDirectionsLanguage("en");
        localRouteParameters.setDirectionsStyleName("NA Desktop");
        if (this.mDescription == null) {
            this.mDescription = getNetworkDescription();
        }
        return localRouteParameters;
    }

    @Override // com.esri.core.tasks.na.RouteTask
    public RouteResult solve(RouteParameters routeParameters) {
        updateDirectionsRes(routeParameters);
        String[] strArr = new String[3];
        double[] dArr = {1.0d, 1.0d, 1.0d};
        String[] augmentAttributes = augmentAttributes(this.mAttributeHelper, routeParameters.getImpedanceAttributeName(), routeParameters.getAccumulateAttributeNames(), strArr, dArr);
        if (strArr[0] != null && augmentAttributes != null) {
            routeParameters.setImpedanceAttributeName(strArr[0]);
            routeParameters.setAccumulateAttributeNames(augmentAttributes);
            ((LocalRouteParameters) routeParameters).setDirectionsTimeAttributeName(strArr[1]);
        }
        RouteResult routeResult = new RouteResult(solve(this.mNativePtr, ((LocalRouteParameters) routeParameters).getNativeHandle(), 0L, routeParameters), routeParameters);
        routeResult.updateConvenience(strArr[1], strArr[2], dArr[0], dArr[1], dArr[2]);
        return routeResult;
    }

    @Override // com.esri.core.tasks.na.RouteTask
    public NetworkDescription getNetworkDescription() throws Exception {
        NetworkDescription networkDescription = this.mDescription;
        if (networkDescription != null) {
            return networkDescription;
        }
        NetworkDescription networkDescription2 = getNetworkDescription(this.mNativePtr);
        networkDescription2.mSupportedDirectionsLanguages = Arrays.asList(directions_languages);
        networkDescription2.mSupportedDirectionsStyles = Arrays.asList(directions_styles);
        this.mDescription = networkDescription2;
        this.mAttributeHelper = createAttributeHelper(this.mNativePtr, null, null, null, null, null);
        return networkDescription2;
    }

    @Override // com.esri.core.tasks.na.RouteTask
    public void dispose() {
        dispose(this.mNativePtr);
        this.mNativePtr = 0L;
        releaseAttributeHelper(this.mAttributeHelper);
        this.mAttributeHelper = 0L;
    }

    protected void finalize() throws Throwable {
        dispose();
        super.finalize();
    }

    private void updateDirectionsRes(RouteParameters routeParameters) {
        String str;
        String str2;
        if (routeParameters.isReturnDirections()) {
            SolveCondition solveCondition = new SolveCondition(SolveCode.INVALID_DIRECTIONS_STYLE);
            SolveCondition solveCondition2 = new SolveCondition(SolveCode.INVALID_DIRECTIONS_LANGUAGE);
            String directionsStyleName = routeParameters.getDirectionsStyleName();
            String generateFallbackLanguage = generateFallbackLanguage(routeParameters.getDirectionsLanguage());
            if (generateFallbackLanguage == null) {
                throw new NetworkAnalystException(new SolveCondition[]{solveCondition2}, routeParameters);
            }
            if (!containsCaseIgnore(directions_styles, directionsStyleName)) {
                throw new NetworkAnalystException(new SolveCondition[]{solveCondition}, routeParameters);
            }
            String str3 = this.mCachedDirectionsStyle;
            if (str3 == null || !str3.equalsIgnoreCase(directionsStyleName) || (str2 = this.mCachedDirectionsLanguage) == null || !str2.equalsIgnoreCase(generateFallbackLanguage)) {
                if ("NA Campus".equalsIgnoreCase(directionsStyleName)) {
                    str = "campus";
                } else {
                    str = "NA Navigation".equalsIgnoreCase(directionsStyleName) ? NotificationCompat.CATEGORY_NAVIGATION : "desktop";
                }
                Class<?> cls = getClass();
                InputStream resourceAsStream = cls.getResourceAsStream("res/" + str + "/" + generateFallbackLanguage + ".json");
                if (resourceAsStream == null) {
                    throw new NetworkAnalystException("Directions file missing");
                }
                Scanner scanner = new Scanner(resourceAsStream);
                String next = scanner.useDelimiter("\\A").next();
                scanner.close();
                setDirectionsResource(this.mNativePtr, next);
                this.mCachedDirectionsStyle = directionsStyleName;
                this.mCachedDirectionsLanguage = generateFallbackLanguage;
            }
        }
    }

    private static String generateFallbackLanguage(String str) {
        String[] strArr;
        String str2 = null;
        if (str == null) {
            return null;
        }
        String str3 = null;
        for (String str4 : directions_languages) {
            if (str4.equalsIgnoreCase(str)) {
                str2 = str4;
            } else if (str.toLowerCase().startsWith(str4.toLowerCase())) {
                str3 = str4;
            }
        }
        return str2 == null ? str3 : str2;
    }

    private static boolean containsCaseIgnore(String[] strArr, String str) {
        if (strArr != null && strArr.length != 0 && str != null) {
            for (String str2 : strArr) {
                if (str.equalsIgnoreCase(str2)) {
                    return true;
                }
            }
        }
        return false;
    }
}
